﻿using System.Web.Http;
using CNav.Common;
using Microsoft.Practices.Unity;
using CNav.Model;
using CNav.Business.Services;
using AutoMapper;

namespace CNav.DataApi.DataApi
{
    public class BaseApiController : ApiController
    {
        [Dependency]
        protected IServiceContainer Service { get; set; }

        private SessionUser _loginUser;
        protected virtual SessionUser LoginUser
        {
            get
            {
                if (_loginUser != null)
                    return _loginUser;

               if (User.Identity.IsAuthenticated)
                {
                    var userProfile = Service.Get<IUserAccountService>().GetUserByEmail(User.Identity.Name);

                    if (userProfile.Data != null)
                    {
                        _loginUser = Mapper.Map<SessionUser>(userProfile.Data);
                        return _loginUser;
                    }
                }
                return null;
            }
        }

        protected virtual bool IsBusinessUser
        {
            get
            {
                var userProfile = LoginUser;
                if (userProfile == null)
                    return false;

                return userProfile.IsBusinessAdmin;
            }
        }
    }
}